define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    // var Controller = {
    //     //文章列表
    //     index: function () {
    //         // 初始化表格参数配置
    //         Table.api.init({
    //             extend: {
    //                 index_url: 'qzworks/qzworks/index?pub_area=1',
    //                 // add_url: 'qzworks/add',
    //                 edit_url: 'qzworks/qzworks/edit',
    //                 del_url: 'qzworks/qzworks/del',
    //                 multi_url: 'qzworks/qzworks/multi',
    //                 works_url:'qzworks/qzworks/index?type=works',
    //                 activity_url:'qzworks/qzworks/index?type=activity',
    //                 table: 'qzworks/qzworks',
    //             }
    //         });
    //
    //         var table = $("#table");
    //
    //         // 初始化表格
    //         table.bootstrapTable({
    //             url: $.fn.bootstrapTable.defaults.extend.index_url,
    //             pk: 'id',
    //             sortName: '',
    //             columns: [
    //                 [
    //                     {checkbox: true},
    //                     {field: 'id', title: __('ID'), sortable: true},
    //                     {field: 'userid', title: __('用户ID'), sortable: true},
    //                     {field: 'img', title: __('头像'), formatter: Table.api.formatter.image, operate: false},
    //                     {field: 'nickname', title: __('昵称'),operate: 'LIKE'},
    //                     {field: 'title', title: __('标题'),operate: 'LIKE'},
    //                     {field: 'content', title: __('内容')},
    //                     {field: 'praise_points', title: __('点赞量'),sortable: true},
    //                     {field: 'collection', title: __('收藏量'),sortable: true},
    //                     {field: 'watch_num', title: __('观看量'),sortable: true},
    //                     {field: 'comment_num', title: __('评论量'),sortable: true},
    //                     {field: 'releast_time', title: __('发布时间'),sortable: true},
    //                     {field: 'pub_area', title: __('发布区域'),visible: false, searchList: {1: __('社区'), 2: __('体验活动')}},
    //                     {field: 'status', title: __('状态'),visible: false, searchList: {0: __('未通过'), 1: __('审核中'), 2: __('通过')}},
    //                     {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
    //                 ]
    //             ]
    //         });
    //
    //         // 为表格绑定事件
    //         Table.api.bindevent(table);
    //     },
    //
    //     add: function () {
    //         Controller.api.bindevent();
    //     },
    //     edit: function () {
    //         Controller.api.bindevent();
    //     },
    //     api: {
    //         bindevent: function () {
    //             Form.api.bindevent($("form[role=form]"));
    //         }
    //     }
    // };

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            $(function () {
                $('.btn-status_refuse').on('click', function () {
                    var params = {status: 0};
                    params['ids'] = [];
                    $("tbody input[type=checkbox]").each(function () {
                        if ($(this).is(':checked')) {
                            params['ids'].push($(this).parent().next().html());
                        }
                    });
                    $.ajax({
                        url: 'qzworks/qzworks/manyUpdateStatus',
                        dataType: 'json',
                        type: 'post',
                        data: params,
                        cache: false,
                        success: function (ret) {
                            if (ret.hasOwnProperty("code")) {
                                var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
                                if (ret.code === 1) {
                                    Toastr.success(msg ? msg : __('操作成功'));
                                } else {
                                    Toastr.error(msg ? msg : __('操作失败'));
                                }
                                setTimeout(function () {
                                    location.reload(ret.url);
                                }, 1000);
                            } else {
                                Toastr.error(__('数据格式有误'));
                            }
                        }, error: function () {
                            Toastr.error(__('网络连接超时'));
                        }
                    });
                });
                $('.btn-status_agree').on('click', function () {
                    var params = {status: 2};
                    params['ids'] = [];
                    $("tbody input[type=checkbox]").each(function () {
                        if ($(this).is(':checked')) {
                            params['ids'].push($(this).parent().next().html());
                        }
                    });
                    $.ajax({
                        url: 'qzworks.qzworks/manyUpdateStatus',
                        dataType: 'json',
                        type: 'post',
                        data: params,
                        cache: false,
                        success: function (ret) {
                            if (ret.hasOwnProperty("code")) {
                                var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
                                if (ret.code === 1) {
                                    Toastr.success(msg ? msg : __('操作成功'));
                                } else {
                                    Toastr.error(msg ? msg : __('操作失败'));
                                }
                                setTimeout(function () {
                                    location.reload(ret.url);
                                }, 1000);
                            } else {
                                Toastr.error(__('数据格式有误'));
                            }
                        }, error: function () {
                            Toastr.error(__('网络连接超时'));
                        }
                    });
                });
            });

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'qzworks/qzworks/index?pub_area=1',
                    extend: {
                        index_url: 'qzworks/qzworks/index?pub_area=1',
                        // add_url: 'qzworks/add',
                        edit_url: 'qzworks/qzworks/edit',
                        del_url: 'qzworks/qzworks/delete',
                        multi_url: 'qzworks/qzworks/multi',
                        table: 'qzworks/qzworks',
                    },
                    toolbar: '#toolbar1',
                    pk: 'id',
                    sortName: '',
                    search: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('ID'), sortable: true},
                            {field: 'userid', title: __('用户ID'), sortable: true},
                            {field: 'img', title: __('头像'), formatter: Table.api.formatter.image, operate: false},
                            {field: 'nickname', title: __('昵称'), operate: 'LIKE'},
                            {field: 'title', title: __('标题'), operate: 'LIKE'},
                            {field: 'content', title: __('内容')},
                            {field: 'praise_points', title: __('点赞量'), sortable: true},
                            {field: 'collection', title: __('收藏量'), sortable: true},
                            {field: 'watch_num', title: __('观看量'), sortable: true},
                            {field: 'comment_num', title: __('评论量'), sortable: true},
                            {
                                field: 'mus_or_video', title: __('素材'), operate: false, formatter: function (value) {
                                if (value.length == 0) {
                                    return "";
                                }
                                if (value[0].indexOf('mp4') > 1) {
                                    return '<video style="width:120px" onclick="showVideo.call(this)" src="' + value[0] + '">您的浏览器不支持video标签</video>';
                                } else {
                                    var img = '';
                                    $.each(value, function (i, v) {
                                        if (i == 0) {
                                            img += '<img style="width: 100px"  onclick="showImg.call(this)" src="' + v + '">';
                                        } else {
                                            img += '<img style="width: 100px;display:none"   src="' + v + '">';
                                        }
                                    })
                                    return img;
                                }
                            }
                            },
                            {field: 'releast_time', title: __('发布时间'), sortable: true},
                            {
                                field: 'status',
                                title: __('状态'),
                                formatter: function (value) {
                                    switch (value) {
                                        case 0:
                                            return "审核未通过";
                                        case 1:
                                            return "审核中";
                                        case 2:
                                            return "通过";
                                    }
                                },
                                searchList: {0: __('未通过'), 1: __('审核中'), 2: __('通过')}
                            },
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table1,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: 'detail',
                                        text: __('查看'),
                                        title: __('查看详情'),
                                        classname: 'btn btn-xs btn-warning btn-addtabs',
                                        icon: 'fa fa-folder-o',
                                        url: 'qzworks/qzworks/detail'
                                    },
                                    {
                                        name: 'edit',
                                        text: __(''),
                                        title: __('通过'),
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                        icon: 'fa fa-check',
                                        confirm: '确定通过该审核吗?',
                                        url: 'qzworks/qzworks/edit_sh?editstate=2',
                                        hidden: function (row) {
                                            if (row.status == '1' || row.status == '2') {
                                                return true;
                                            }
                                        },
                                        success: function (msg) {
                                            Layer.alert(msg);
                                            //如果需要阻止成功提示，则必须使用return false;
                                            //return false;
                                        },
                                        error: function (data, ret) {
                                            console.log(data, ret);
                                            Layer.alert(ret.msg);
                                            return false;
                                        }
                                    },
                                    {
                                        name: 'edit',
                                        text: __(''),
                                        title: __('驳回'),
                                        classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                        icon: 'fa fa-close',
                                        confirm: '确定驳回该文章吗?',
                                        url: 'qzworks/qzworks/edit_sh?editstate=0',
                                        hidden: function (row) {
                                            if (row.status == '1' || row.status == '0') {
                                                return true;
                                            }
                                        }
                                    },
                                ],
                                formatter: Table.api.formatter.operate
                            },
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'qzworks/qzworks/index?pub_area=2',
                    extend: {
                        index_url: 'qzworks/qzworks/index?pub_area=1',
                        // add_url: 'qzworks/add',
                        edit_url: 'qzworks/qzworks/edit',
                        del_url: 'qzworks/qzworks/del',
                        multi_url: 'qzworks/qzworks/multi',
                        table: 'qzworks/qzworks',
                    },
                    toolbar: '#toolbar2',
                    pk: 'id',
                    sortName: '',
                    search: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('ID'), sortable: true},
                            {field: 'userid', title: __('用户ID'), sortable: true},
                            {field: 'img', title: __('头像'), formatter: Table.api.formatter.image, operate: false},
                            {field: 'nickname', title: __('昵称'), operate: 'LIKE'},
                            {field: 'title', title: __('标题'), operate: 'LIKE'},
                            {field: 'content', title: __('内容')},
                            {field: 'praise_points', title: __('点赞量'), sortable: true},
                            {field: 'collection', title: __('收藏量'), sortable: true},
                            {field: 'watch_num', title: __('观看量'), sortable: true},
                            {field: 'comment_num', title: __('评论量'), sortable: true},
                            {
                                field: 'mus_or_video', title: __('素材'), operate: false, formatter: function (value) {
                                if (value == "") {
                                    return "";
                                }
                                if (value[0].indexOf('mp4') > 1) {
                                    return '<video style="width:120px" onclick="showVideo.call(this)" src="' + value[0] + '">您的浏览器不支持video标签</video>';
                                } else {
                                    var img = '';
                                    $.each(value, function (i, v) {
                                        if (i == 0) {
                                            img += '<img style="width: 100px"  onclick="showImg.call(this)" src="' + v + '">';
                                        } else {
                                            img += '<img style="width: 100px;display:none"   src="' + v + '">';
                                        }
                                    })
                                    return img;
                                }
                            }
                            },
                            {field: 'releast_time', title: __('发布时间'), sortable: true},
                            {
                                field: 'status',
                                title: __('状态'),
                                formatter: function (value) {
                                    switch (value) {
                                        case 0:
                                            return "审核未通过";
                                        case 1:
                                            return "审核中";
                                        case 2:
                                            return "通过";
                                    }
                                },
                                searchList: {0: __('未通过'), 1: __('审核中'), 2: __('通过')}
                            },
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table2,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: 'detail',
                                        text: __('查看'),
                                        title: __('查看详情'),
                                        classname: 'btn btn-xs btn-warning btn-addtabs',
                                        icon: 'fa fa-folder-o',
                                        url: 'qzworks/qzworks/detail'
                                    },
                                    {
                                        name: 'edit',
                                        text: __(''),
                                        title: __('通过'),
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                        icon: 'fa fa-check',
                                        confirm: '确定通过该审核吗?',
                                        url: 'qzworks/qzworks/edit_sh?editstate=2',
                                        hidden: function (row) {
                                            if (row.status == '1') {
                                                return true;
                                            }
                                        }
                                    },
                                    {
                                        name: 'edit',
                                        text: __(''),
                                        title: __('驳回'),
                                        classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                        icon: 'fa fa-close',
                                        confirm: '确定驳回该文章吗?',
                                        url: 'qzworks/qzworks/edit_sh?editstate=0',
                                        hidden: function (row) {
                                            if (row.status == '1') {
                                                return true;
                                            }
                                        }
                                    },
                                ],
                                formatter: Table.api.formatter.operate
                            },

                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});


function showImg() {
    var img = [];
    $(this).parent().find('img').each(function () {
        img.push({'src': $(this).attr('src')});
    });

    layer.photos({
        photos: {'data': img}, //格式见API文档手册页
        anim: img.length - 1 //0-6的选择，指定弹出图片动画类型，默认随机
    });
}

function showVideo() {
    layer.open({
        type: 2,
        title: false,
        area: ['80%', '80%'],
        shade: 0.8,
        closeBtn: 0,
        shadeClose: true,
        content: $(this).attr('src')
    });
}